<template>
  <div class="app-container">
    <el-form
      ref="userForm"
      :model="userForm"
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item>
        <el-upload
          class="avatar-uploader"
          action="http://localhost:8090/file/upload"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :show-file-list="false"
        >
          <el-avatar
            shape="square"
            alt="点击上传头像"
            :size="150"
            :src="userForm.userImg"
          />
        </el-upload>
      </el-form-item>
      <el-form-item label="用户名称">
        <el-input v-model="userForm.userName" disabled />
      </el-form-item>
      <el-form-item label="用户昵称" prop="trueName">
        <el-input v-model="userForm.trueName" placeholder="请输入昵称" />
      </el-form-item>
      <el-form-item label="用户邮箱" prop="userNmail">
        <el-input v-model="userForm.userEmail" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="用户性别">
        <el-select
          v-model="userForm.userSex"
          style="width: 100%"
          placeholder="请选择性别"
        >
          <el-option
            v-for="item in sexOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-input v-model="userForm.createTime" disabled />
      </el-form-item>
      <el-form-item>
        <el-button @click="$router.go(-1)">返回</el-button>
        <el-button @click="resetForm('userForm')">重置</el-button>
        <el-button type="primary" @click="submitForm('userForm')"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import userApi from '@/api/sys/user'
import { getInfo } from '@/api/login'
import { getToken } from '@/utils/auth'

export default {
  data() {
    return {
      sexOptions: [
        { value: '男', label: '男' },
        { value: '女', label: '女' },
        { value: '未知', label: '未知' }
      ],
      userForm: {
        id: undefined,
        userName: '',
        trueName: '',
        userImg: '',
        userEmail: '',
        userSex: '',
        createTime: ''
      },
      rules: {
        nickName: [
          { required: true, message: '用户昵称不能为空', trigger: 'blur' }
        ],
        userEmail: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          {
            type: 'email',
            message: '邮箱格式错误',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    // 获取用户信息
    getUserInfo() {
      getInfo(getToken()).then((res) => {
        this.userForm = res.data
      })
    },

    // 提交
    submitForm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          userApi.updateProfile(this.userForm).then(() => {
            this.$message.success('修改成功')
          })
        }
      })
    },

    // 重置
    resetForm(form) {
      this.$refs[form].resetFields()
      this.getUserInfo()
    },

    // 头像上传成功
    handleAvatarSuccess(res, file) {
      console('file success')
      console.log(res, file)
    },

    // 头像上传前
    beforeAvatarUpload(file) {
      console.log('file before')
      console.log(file)
    }
  }
}
</script>
